layui.use(['form','layer','upload','laydate','treeSelect','layedit','tableSelect','table'],function(){
        var form = layui.form,
        layer = parent.layer === undefined ? layui.layer : top.layer,
            $ = layui.jquery;
    var upload = layui.upload;
    var laydate = layui.laydate;
    var treeSelect= layui.treeSelect;//树形下拉框



    treeSelect.render({
        elem: '#categoryName',
        data: '/admin/facilityCategory/treelist',
        type: 'get',
        placeholder: '默认添加首级分类',// 占位符
        search: true,// 是否开启搜索功能：true/false，默认false
        click: function(data){
            $.get("/admin/facility/getFacilityListByCateNotLay?page=1&limit=10000&categoryId="+data.current.id,function(d) {
                $("#facilityId").html("");
                var optionstring = "";
                $.each(d, function(i,item){
                    optionstring += "<option value=\"" + item.facilityId + "\" >" + item.facilityId + "</option>";
                });
                $("#facilityId").html(optionstring);
                var obj=$("#facilityId option")
                for(i=0;i<obj.length;i++) {//下拉框的长度就是它的选项数.
                    if(obj[i].selected==true) {
                        $("#facilityId").val(obj[i].value)
                        // $.get("/admin/facility/getFacility?facilityId="+obj[i].value,function(d) {
                        //     $("#unit").val(d.unit)
                        // });
                    }
                }
                form.on('select(test)', function(data){
                    $.get("/admin/facility/getFacility?facilityId="+data.value,function(d) {
                        $("#facilityId").val(data.value)
                        // $("#unit").val(d.unit)
                    });
                });

                form.render('select');
            });
        },
        success: function (data) { // 加载完成后的回调函数
            $.get("/admin/facility/getFacilityListByCateNotLay?page=1&limit=10000&categoryId="+data.data[0].id,function(d) {
                $("#facilityName").html("");
                var optionstring = "";
                $.each(d, function(i,item){
                    optionstring += "<option value=\"" + item.facilityId + "\" >" + item.facilityName + "</option>";
                });

                $("#facilityName").html(optionstring);

                var obj=$("#facilityName option")
                for(i=0;i<obj.length;i++) {//下拉框的长度就是它的选项数.
                    if(obj[i].selected==true) {
                        $("#facilityId").val(obj[i].value)
                        // $.get("/admin/facility/getFacility?facilityId="+obj[i].value,function(d) {
                        //     $("#unit").val(d.unit)
                        // });
                    }
                }
                form.on('select(test)', function(data){
                    $.get("/admin/facility/getFacility?facilityId="+data.value,function(d) {
                        $("#facilityId").val(data.value)
                        // $("#unit").val(d.unit)
                    });

                });
                form.render('select');
            });

        }
    });

    var date = new Date();
    laydate.render({
        elem: '#useDate'
        ,type:'datetime'
        ,range: true
        //,format:'yyyy-MM-dd HH:mm:ss - yyyy-MM-dd HH:mm:ss'
        ,value: date.getFullYear() + '-' + (date.getMonth() + 1) + '-' + date.getDate() + " " + date.getHours() + ":" + date.getMinutes() + ":" + date.getSeconds()+" - "+date.getFullYear() + '-' + (date.getMonth() + 1) + '-' + date.getDate() + " " + date.getHours() + ":" + date.getMinutes() + ":" + date.getSeconds()  //必须遵循format参数设定的格式  + " " + date.getHours() + ":" + date.getMinutes() + ":" + date.getSeconds()
    });


   /* laydate.render({
        elem: '#returnDate'
        ,type:'datetime'
        ,value: date.getFullYear() + '-' + (date.getMonth() + 1) + '-' + date.getDate() + " " + date.getHours() + ":" + date.getMinutes() + ":" + date.getSeconds()  //必须遵循format参数设定的格式  + " " + date.getHours() + ":" + date.getMinutes() + ":" + date.getSeconds()
    });*/
form.on("submit(addFacility)",function(data){
        //弹出loading
        var index = top.layer.msg('数据提交中，请稍候',{icon: 16,time:false,shade:0.8});
     $.post("/admin/facilityUsed/bookAdd",{
         facilityId: $("#facilityId").val(),
         categoryId: $("#categoryId").val(),
         userId: $("#userId").val(),
         // useNum: $("#useNum").val(),
         useDate : new Date(Date.parse($("#useDate").val().split(" - ")[0])),
         returnDate : new Date(Date.parse($("#useDate").val().split(" - ")[1])),
         },
         function(res){
         console.log(res)
            //成功后返回的数据res，这里可以写一些成功后的操作
            if(res.code==100) {
                top.layer.msg("预约失败！该时间段已经有人预约！");
                return false;
            }else if (res.code == 200){
                top.layer.close(index);
                top.layer.msg("预约成功！");
                layer.closeAll("iframe");
                //刷新父页面
                parent.location.reload();
                return true;
            }else{
                top.layer.msg("预约成败！");
                return false;
            }

         })

        return false;
    })

})